//
//  APDiscoveryDataManager.m
//  AqualPolo
//
//  Created by dong on 2024/5/4.
//

#import "APDiscoveryDataManager.h"
#import "APBroadcasterModel.h"
@implementation APDiscoveryDataManager
+ (APDiscoveryDataManager *)shareInstance{
    static APDiscoveryDataManager *shareId = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareId = [[self alloc] init];
    });
    return shareId;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)blockUserWorks:(NSString *)userId{
    if (self.dataArray.count > 0) {
        for (APWorksModel *worksModel in self.dataArray) {
            if ([worksModel.userId isEqualToString:userId]) {
                worksModel.isBlock = true;
            }
        }
    }
   NSArray *res = [self.dataArray yy_modelToJSONObject];
    [self saveInfo:res toFile:worksListFile];
}

- (void)unBlockUserWorks:(NSString *)userId{
    if (self.dataArray.count > 0) {
        for (APWorksModel *worksModel in self.dataArray) {
            if ([worksModel.userId isEqualToString:userId]) {
                worksModel.isBlock = false;
            }
        }
    }
    NSArray *res = [self.dataArray yy_modelToJSONObject];
    [self saveInfo:res toFile:worksListFile];
}


- (NSArray<APCommentModel *> *)loadCommentsByWorksId:(NSString *)worksId{
    NSString *filePath = [NSString stringWithFormat:@"%@/works_comment_%@",worksCommentsFiles,worksId];
     NSArray *result = [self readFileDataAtPath:filePath];
    if (result && result.count > 0) {
        NSMutableArray *comments = [NSMutableArray array];
        for (NSDictionary *dict in result) {
            APCommentModel *model = [APCommentModel yy_modelWithDictionary:dict];
            [comments addObject:model];
        }
        return comments;
    }
    return @[];
}

- (void)saveCommentsWorksId:(NSString *)worksId comments:(NSArray<APCommentModel *> *)comments{
    NSString *filePath = [NSString stringWithFormat:@"%@/works_comment_%@",worksCommentsFiles,worksId];
    if (comments.count > 0) {
        NSArray *result = [comments yy_modelToJSONObject];
        if (result) {
            [self saveInfo:result toFile:filePath];
        }
    }else{
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        }
    }
}

- (void)addWorksData:(APWorksModel *)worksModel{
    [self.dataArray insertObject:worksModel atIndex:0];
    NSArray *result = [self.dataArray yy_modelToJSONObject];
    [self saveInfo:result toFile:worksListFile];
}

- (void)updateWorks:(NSString *)worksId{
    NSArray *result = [self.dataArray yy_modelToJSONObject];
    [self saveInfo:result toFile:worksListFile];
}

- (void)deleteWorks:(APWorksModel *)model{
    [self.dataArray removeObject:model];
    NSArray *result = [self.dataArray yy_modelToJSONObject];
    [self saveInfo:result toFile:worksListFile];
}

- (void)loadData:(APDiscoveryDataBlock)callBack{
    if (self.dataArray.count > 0) {
        callBack();
        return;
    }
    LYLog(@"%@",worksListFile);
   NSArray *result = [self readFileDataAtPath:worksListFile];
    if (result && result.count > 0) {
        for (NSDictionary *dict in result) {
            APWorksModel *model = [APWorksModel yy_modelWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        callBack();
        return;
    }
    WS(ws);
    [[APUserDataManager shareInstance] loadUsers:^(NSArray * _Nonnull users) {
        if (users.count > 0) {
            [ws makeWorksData:users result:callBack];
        }else{
            callBack();
        }
    }];
}
     
     

- (void)makeWorksData:(NSArray *)users result:(APDiscoveryDataBlock)callBack{
    NSArray *localUsers = [self loadBundleFile];
    for (NSInteger i = 0; i < localUsers.count; i++) {
        APUserWorksModel *localUserModel = localUsers[i];
        APBroadcasterModel *userModel = users[i];
        for (APWorksModel *worksModel in localUserModel.works) {
            worksModel.userId = userModel.userId;
            worksModel.avatar = userModel.avatar;
            worksModel.nickname = userModel.nickname;
            worksModel.followNum = arc4random() % 20 + 5;
            worksModel.upvote = false;
            worksModel.ballType = arc4random() % 6 + 1;
            [self.dataArray addObject:worksModel];
        }

    }
    NSArray *res = [self.dataArray yy_modelToJSONObject];
    [self saveInfo:res toFile:worksListFile];
    LYLog(@"%@",worksListFile);
    callBack();
}
     


- (NSArray *)loadBundleFile{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"discoverys" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSError *error = nil;
    NSArray *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSMutableArray *list = [NSMutableArray array];
    if (!error) {
//        LYLog(@"%@",result);
        for (NSDictionary *dict in result) {
            APUserWorksModel *model = [APUserWorksModel yy_modelWithDictionary:dict];
            [list addObject:model];
        }
    }else{
        LYLog(@"%@",error);
    }
    return list;
}

- (void)clearData{
    [self.dataArray removeAllObjects];
    [APDiscoveryDataManager removeFile:worksListFile];
    [APDiscoveryDataManager removeFile:worksCommentsFiles];
    [APDiscoveryDataManager removeFile:VOICE_RECORDFILE_FOLDER_PATH];    
}




@end
